# -*- coding:utf-8 -*-
# @Author   : ZGS
# @Time     : 2021/1/4 18:27
# @FileName : xlwt_.py
# 我拿青春赌明天!
import xlwt
import datetime

"""
# 例子:
# 实例化
log = LogData(sheet_name="data", file_name="commits_data.xlsx")
# 创建sheet
sheet = None
for r in res:
    print(r)
    # 获取 dict 数据
    if sheet is None:
        # 创建表头
        sheet = log.create_sheet([col for col in r.keys()])
        # 插入数据
        log.insert_sheet(sheet, data=r)
    else:
        log.insert_sheet(sheet, data=r)
log.save_excel()
"""


class LogData(object):
    # 时间 style
    style = xlwt.XFStyle()
    style.num_format_str = "YYYY-MM-DD hh:mm:ss"
    book = xlwt.Workbook()
    x = y = 0
    add_x = 1

    def __init__(self, sheet_name, file_name):
        '''
        实例化
        :param sheet_name: sheet名
        :param file_name: excel文件名
        '''
        self.sheet_name = sheet_name
        self.file_name = file_name

    def create_sheet(self, cols=None):
        """
        创建表
        :param cols: 表头
        :return:
        """
        if isinstance(cols, list):
            sheet = self.book.add_sheet(self.sheet_name)
            for index, col in enumerate(cols):
                sheet.write(self.x, self.y+index, col)

            return sheet

    def insert_sheet(self, sheet, data):
        """
        插入数据
        :param sheet:
        :param data:
        :return:
        """
        if sheet:
            if isinstance(data, dict):
                sycle_y = 0
                for k, v in data.items():
                    if isinstance(v, str):
                        sheet.write(self.x+self.add_x, self.y+sycle_y, v)
                    elif isinstance(v, dict):
                        sheet.write(self.x+self.add_x, self.y+sycle_y, str(v))
                    elif isinstance(v, list):
                        sheet.write(self.x+self.add_x, self.y+sycle_y, str(v))
                    elif isinstance(v, tuple):
                        sheet.write(self.x + self.add_x, self.y + sycle_y, str(v))
                    elif isinstance(v, datetime.datetime):
                        sheet.write(self.x+self.add_x, self.y+sycle_y, v, self.style)
                    else:
                        sheet.write(self.x + self.add_x, self.y + sycle_y, v)
                    sycle_y += 1
                self.add_x += 1
            elif isinstance(data, list):
                for index, v in data:
                    if isinstance(v, str):
                        sheet.write(self.x + self.add_x, self.y + index, v)
                    elif isinstance(v, dict):
                        sheet.write(self.x + self.add_x, self.y + index, str(v))
                    elif isinstance(v, list):
                        sheet.write(self.x + self.add_x, self.y + index, str(v))
                    elif isinstance(v, datetime.datetime):
                        sheet.write(self.x + self.add_x, self.y + index, v, self.style)
                    else:
                        sheet.write(self.x + self.add_x, self.y + index, v)
                self.add_x += 1

    def save_excel(self):
        self.book.save(self.file_name)
